﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using ZLDC.Common.Estate;
using System.Data;

namespace ZLDC.Estate.TM
{
    public partial class UserTelList : _PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AddDeptTree();
                hiddDeptId.Value = _Utility.GetInfoBySession().m_sDeptID;//部门ID
                this.hlkDept.Text = _Utility.GetInfoBySession().m_sDeptName + "↓";//部门名称
                PageInit();
            }
               
        }
        private void AddDeptTree()
        {
            string sTopDept = this.info.m_sDeptID;//默认取表中第一行为最上层部门
            string deptid_list = CacheUtility.Cache_GetAllChildDept(this.info.m_sDeptID); 

            sTopDept = ConfigurationSettings.AppSettings["SalesDepartment"].ToString().Trim();//最上层部门

            ZLDC.Topo.BLL.Estate.DeptTree zdt = new ZLDC.Topo.BLL.Estate.DeptTree(sTopDept, deptid_list);
            ltlDeptTree.Text = zdt.GetZlhqDept();
        }
        private void PageInit()
        {

            string sSql = "select v.*,t.name as deptname From  v_s_usr_lp as v inner join t_zlhq_t_s_alldept as t  on v.t_id=t.Id where v.dltflg=0 and v.status<>'离职' and v.status<>'停薪留职'";

            if (this.tbKey.Text.Trim() != "")
            {
                string sNo, sName;
                _Utility.GetUserId(tbKey.Text, out sName, out sNo);
                if (sName == "")
                {
                    sSql += " and (v.name like " + "'%" + sNo + "%'" + " or v.tel like " + "'%" + this.tbKey.Text + "%'" + " or v.curtmobile like " + "'%" + this.tbKey.Text + "%'" + ")";
                }
                else 
                {
                    sSql += " and (v.name like " + "'%" + sName + "%'"+ ")";
 
                }
            }
            else if (this.rbSel.Checked)
            {
                if (_Utility.GetInfoBySession().m_sStation == "区域经理")
                {
                    string deptid_list = CacheUtility.Cache_GetAllChildDept(this.hiddDeptId.Value);
                    sSql += " and t_id in(" + deptid_list + ")";
                }
                else
                    sSql += " and t_id = " + this.hiddDeptId.Value;
            }
            else
            {
                sSql += " and t_id in (select id from t_zlhq_t_s_alldept where dltflg = 0 and name = '" +this.tbDeptInput.Text.Trim() + "')";
            }
            DataSet ds = null;
            using (_Database db = new _Database("zlhq_user")) 
            {
                ds = db.Query(sSql);
            }
                gvArea.DataSource = ds;
                gvArea.DataBind();

           
        }
        protected void gvArea_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标经过时，行背景色变 
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
                //鼠标移出时，行背景色变 
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");


            }
        }
        // 用户管理分页
        protected void gvArea_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvArea.PageIndex = e.NewPageIndex;
            PageInit();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageInit();
        }
    }
}
